package com.black.knight.chess.adapters;

import android.graphics.PorterDuff.Mode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.black.knight.chess.R;
import com.black.knight.chess.StartPositionActivity;
import com.black.knight.chess.model.SPModel;
import com.black.knight.chess.model.SahModel;

public class SPTableAdapter extends BaseAdapter {
    private StartPositionActivity mContext;
    
    public static boolean animate = true;

    public SPTableAdapter(StartPositionActivity c) {
        mContext = c;
    }

    public int getCount() {
        return SahModel.getInstance().getCount();
    }

    public Integer getItem(int position) {
        return SahModel.getInstance().getItem(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {    	

    	final ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
        	imageView = (ImageView) LayoutInflater.from(mContext).inflate(R.layout.figure, parent, false);
        	
        	imageView.setLayoutParams(new AbsListView.LayoutParams(SahModel.getTableWidth() / 8, SahModel.getTableWidth() / 8));
        } else {
            imageView = (ImageView) convertView;
        }

        if(position == SPModel.getInstance().getSelectedFlagTable()) {
        	imageView.setColorFilter(mContext.getResources().getColor(R.color.green), Mode.DST_ATOP);
        } else {
        	imageView.setColorFilter(null);
        }
        
        imageView.setImageResource(SPModel.getInstance().getFields()[position]);
                        
        return imageView;
    }
}